package com.imooc.food.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.imooc.food.dto.OrderMessageDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * ProjectName food
 *
 * @author xieyucan
 * <br>CreateDate 2022/9/22 17:55
 */
@Slf4j
@Service
public class RabbitMessageService {

  ObjectMapper objectMapper=new ObjectMapper();

  @RabbitListener(queues = "my.queue")
  public void handleMessagePractice(@Payload Message message) throws IOException {
      OrderMessageDTO orderMessageDTO = objectMapper.readValue(message.getBody(), OrderMessageDTO.class);
      log.info("my.queue,orderId:{},accountId:{}",
              orderMessageDTO.getOrderId(),orderMessageDTO.getAccountId());
  }

    @RabbitListener(queues = "my.fanout")
    public void handleMessageFanout(@Payload Message message) throws IOException {
        OrderMessageDTO orderMessageDTO = objectMapper.readValue(message.getBody(), OrderMessageDTO.class);
        log.info("my.fanout,orderId:{},accountId:{}",
                orderMessageDTO.getOrderId(),orderMessageDTO.getAccountId());
    }

    @RabbitListener(queues = "my.fanout1")
    public void handleMessageFanout1(@Payload Message message) throws IOException {
        OrderMessageDTO orderMessageDTO = objectMapper.readValue(message.getBody(), OrderMessageDTO.class);
        log.info("my.fanout1,orderId:{},accountId:{}",
                orderMessageDTO.getOrderId(),orderMessageDTO.getAccountId());
    }
}
